﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace UFrame
{
    /// <summary>
    /// IO常用库
    /// </summary>
    public static class IOTool
    {
        public static string CombinePath(params string[] path)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < path.Length - 1; i++)
            {
                string a = path[i];
                string b = path[i + 1];

                if (a.EndsWith("/") || a.EndsWith(@"\"))
                {
                    if ((b.StartsWith("/") || b.StartsWith(@"\")))
                    {
                        int len = sb.Length - 1;
                        if (len <= -1)
                            len = a.Length - 1;
                        sb.Append(a.Substring(0, len));
                    }
                    else
                    {
                        sb.Append(a + b);
                    }
                }
                else
                {
                    if ((b.StartsWith("/") || b.StartsWith(@"\")))
                    {
                        if (i == path.Length - 2)
                        {
                            sb.Append(a + b);
                        }
                        else
                        {
                            sb.Append(a);
                        }
                    }
                    else
                    {
                        if (b.StartsWith("/"))
                        {
                            sb.Append(a + "/");
                        }
                        else if (b.StartsWith(@"\"))
                        {
                            sb.Append(a + @"\");
                        }
                        else if (!(b.StartsWith("/") || b.StartsWith(@"\")))
                        {
                            sb.Append(a + @"\" + b);
                        }
                        else
                        {
                            sb.Append(a + @"\");
                        }
                    }
                }
            }

            return sb.ToString();
        }

        public static string Polishing(string path)
        {

            if (path.EndsWith("/"))
                return path;
            if (path.EndsWith("\\"))
                return path;
            
            return path + "/";
        }

        public static Dictionary<string, string> AnalyzeTxtToDic(string path)
        {
            Encoding en = Encoding.Unicode;
            char fuhao = ':';
            return AnalyzeTxtToDic(path, en, fuhao);
        }

        public static Dictionary<string, string> AnalyzeTxtToDic(string path, Encoding en)
        {
            char fuhao = ':';
            return AnalyzeTxtToDic(path, en, fuhao);
        }

        public static Dictionary<string, string> AnalyzeTxtToDic(string path, Encoding en, char fuhao)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string content = File.ReadAllText(path, en);
            string[] lines = content.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            foreach (var line in lines)
            {
                string[] ary = line.Split(fuhao);
                string key = ary[0].Trim();
                string value = ary[1].Trim();

                if (ary.Length == 2)
                {
                    dic.Add(key, value);
                }

                if (ary.Length == 3)
                {
                    dic.Add(key, value);
                }
            }

            return dic;
        }


        public static bool CopyDirectory(string sourcePath, string destinationPath, bool overwriteExisting)
        {
            bool ret = false;
            try
            {
                sourcePath = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
                destinationPath = destinationPath.EndsWith(@"\") ? destinationPath : destinationPath + @"\";

                if (Directory.Exists(sourcePath))
                {
                    if (Directory.Exists(destinationPath) == false)
                    {
                        Directory.CreateDirectory(destinationPath);
                    }

                    foreach (string fls in Directory.GetFiles(sourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(destinationPath + flinfo.Name, overwriteExisting);
                    }

                    foreach (string drs in Directory.GetDirectories(sourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, destinationPath + drinfo.Name, overwriteExisting) == false)
                        {
                            ret = false;
                        }
                    }
                }

                ret = true;
            }
            catch (Exception ex)
            {
                ret = false;
            }

            return ret;
        }


        public static bool DeleteAllFile(string path)
        {
            if (!Directory.Exists(path))
                return true;
            
            DirectoryInfo dir = new DirectoryInfo(path);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo) //判断是否文件夹
                {
                    DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                    subdir.Delete(true); //删除子目录和文件
                }
                else
                {
                    if (File.Exists(i.FullName))
                    {
                        File.Delete(i.FullName); //删除指定文件
                    }
                }
            }

            return true;
        }
    }
}